package com.example.mybatisplus.tag.controller;

import com.example.mybatisplus.dynamic.DynamicDataSource;
import com.example.mybatisplus.tag.domain.User;
import com.example.mybatisplus.tag.domain.UserInfo;
import com.example.mybatisplus.tag.service.MybatisTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * mybatis 标签学习
 * 1.除了常见crud标签外，select,insert,update,delete
 *
 * @Author longdeng
 * @Date 2020/9/21 0021 20:33
 */
@Controller
@RequestMapping("/api/tag")
public class MybatisTagController {

    @Autowired
    private MybatisTagService mybatisTagService;

    /**
     * resultMap标签学习 - 个人感觉作用不大
     *
     * @return
     */
    @RequestMapping("resultMap")
    @ResponseBody
    public String resultMap() {
        List<UserInfo> userList = mybatisTagService.resultMap();
        return "resultMap";
    }

    /**
     * if标签的使用
     *
     * @return
     */
    @RequestMapping("ifTag")
    @ResponseBody
    public Object ifTag(@RequestParam Map<String, Object> map) {
        List<User> userList = mybatisTagService.ifTag(map);
        return userList;

    }

    /**
     * foreach标签测试
     *
     * @return
     */
    @RequestMapping("foreachTag")
    @ResponseBody
    public Object foreachTag(@RequestParam Map<String, Object> map) {
        List<User> userList = mybatisTagService.foreachTag(map);
        return userList;

    }

    /**
     * choose标签测试
     *
     * @return
     */
    @RequestMapping("chooseTag")
    @ResponseBody
    public Object chooseTag(@RequestParam Map<String, Object> map) {
        return mybatisTagService.chooseTag(map);
    }

    /**
     * set标签测试使用
     *
     * @return
     */
    @RequestMapping("setTag")
    @ResponseBody
    public Object setTag(@RequestParam Map<String, Object> map) {
        mybatisTagService.setTag(map);
        return 1;
    }

    /**
     * trim标签测试使用
     *
     * @return
     */
    @RequestMapping("trimTag")
    @ResponseBody
    public Object trimTag(@RequestParam Map<String, Object> map) {
        List<User> userList = mybatisTagService.trimTag(map);
        return userList;
    }

    /**
     * include + sql 标签测试使用
     *
     * @return
     */
    @RequestMapping("includeTag")
    @ResponseBody
    @DynamicDataSource(value = "ds1")
    public Object includeTag(@RequestParam Map<String, Object> map) {
        List<User> userList = mybatisTagService.includeTag(map);
        return userList;
    }

    /**
     * 使用foreach标签 批量生成数据
     * foreach，sql，include
     * 算是几天学习标签的综合使用吧
     *
     * @return
     */
    @RequestMapping("addUsers")
    @ResponseBody
    @DynamicDataSource(value = "ds2")
    public Object addUsers(@RequestParam Map<String, Object> map) {
        return mybatisTagService.addUsers(map);
    }


}
